Skip to content

Track $arr[$key] existence across array_search/array_find_key via conditional expression holders#5552

Open
phpstan-bot wants to merge 9 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-bcl4slb
Open

Track $arr[$key] existence across array_search/array_find_key via conditional expression holders#5552
phpstan-bot wants to merge 9 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-bcl4slb

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

@phpstan-bot phpstan-bot commented Apr 27, 2026

similar to #5537

Fixes phpstan/phpstan#14537

Comment thread src/Analyser/TypeSpecifier.php Outdated
@staabm
Copy link
Copy Markdown
Contributor

staabm commented Apr 27, 2026

can we do similar stuff for array_rand?

@staabm staabm force-pushed the create-pull-request/patch-bcl4slb branch from a413d3c to af34f1f Compare April 29, 2026 05:01
@staabm
Copy link
Copy Markdown
Contributor

staabm commented Apr 29, 2026

rebase the pull request and resolve conflicts

@clxmstaab clxmstaab force-pushed the create-pull-request/patch-bcl4slb branch from 02ca2ee to 67c9b37 Compare April 29, 2026 10:13
@staabm staabm requested a review from VincentLanglet April 29, 2026 12:14
Comment thread src/Analyser/TypeSpecifier.php
Comment thread src/Analyser/TypeSpecifier.php Outdated
(new SpecifiedTypes([], []))->setNewConditionalExpressionHolders([
$dimFetchString => [$holder->getKey() => $holder],
]),
$this->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For array_search we could improve the type no ?

If

$key = array_search($needle, $arr)

then

$arr[$key] = $needle

https://phpstan.org/r/0c453b65-b551-42b6-920e-510e0488a55e

@VincentLanglet VincentLanglet requested a review from staabm April 29, 2026 19:10
staabm and others added 7 commits April 30, 2026 07:37
…ia conditional expression holders

- Add conditional expression holders in TypeSpecifier for `$key = array_search($needle, $arr)` that fire when `$key !== false`, registering `$arr[$key]` as existing
- Add conditional expression holders for `$key = array_find_key($arr, $cb)` that fire when `$key !== null`, registering `$arr[$key]` as existing
- Add `array_find_key` to the existing `array_key_first/last !== null` comparison handler to narrow array to non-empty
- Move `array_search` true-context handling from standalone block into unified handler alongside the conditional holder logic
- Update existing test that was asserting the buggy behavior (separate assignment `$key = array_search(...)` followed by `if ($key !== false)` was reporting "Offset might not exist")
… tracking

Extract createArrayDimFetchConditionalExpressionHolder() helper method and
merge the separate array_search/array_find_key blocks into one, reducing
three instances of the same ~12-line pattern to single-line calls.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…st instead of duplicating

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…le type for strict array_search

Both array_search and array_find_key now narrow the array to non-empty
when the result is not the sentinel value (false/null respectively).

For array_search with strict mode (third argument true), $arr[$key] is
narrowed to the intersection of the needle type and the value type,
since strict comparison guarantees the found value has the same type as
the needle.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@staabm staabm force-pushed the create-pull-request/patch-bcl4slb branch from ed8c14b to b8de415 Compare April 30, 2026 05:37
@staabm
Copy link
Copy Markdown
Contributor

staabm commented Apr 30, 2026

@VincentLanglet simplified it a bit more. I think its good to go

@staabm staabm force-pushed the create-pull-request/patch-bcl4slb branch from a2621b7 to 3c1137b Compare April 30, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants